<?php
if(empty($_POST)) {
Header("Location: ../index.html");
exit(); 
}
?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>RARAR</title>
        <link type="text/css" href="../css/custom-theme/jquery-ui-1.8.18.custom.css" rel="stylesheet" />	
        <link type="text/css" href="../css/rarar.css" rel="stylesheet" />
        <script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="../js/jquery-ui-1.8.18.custom.min.js"></script>
        <script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script>
        <script  type="text/javascript" src="../js/result.js"></script>
    </head>

<?php
/*IWA project - result.php
Maximilian Einoeder
Mark Mensing
 */

include 'RARAR.php';


//Get data posted from the form
$from = $_POST['inFrom'];
$to = $_POST['inTo'];
$date = $_POST['inDate'];
$time = $_POST['inHour'].':'.$_POST['inMinute'];

$myrarar = new RARAR($from,$to, $date, $time);

$trainError = '';

try {
$trainStops = $myrarar->getTrainconnection();
$railresult = $myrarar->get_railresult($trainStops,array("AMUS","STDM","HSTS"));
}
 catch (Exception $e) {
     $trainError .= $e->getMessage();
 }
$roadresult = $myrarar->get_result(array("AMUS","STDM","HSTS"));


?>


    <body onload="initialize()">
    <!--Hidden form to be able to use the data from the previous page in javascript-->
    <form>
        <input type="hidden" id='inputFrom' value="<?echo $from?>"/>
        <input type="hidden" id='inputTo' value="<?echo $to?>"/>
        <input type="hidden" id='inputDate' value="<?echo $date?>"/>
        <input type="hidden" id='inputTime' value="<?echo $time?>"/>
    </form>
	<div class="mainDiv2">
		<div class="container">
			<div class="logo" />
		</div>
		<div class="container">
			<div class="headertext">
				<div class="header1">Your connection request</div>
				<p class="text">From <b><? echo $from; ?></b> to <b><? echo $to; ?></b> on the <b><? echo $date; ?></b> at <b><? echo $time; ?></b>. <i><a href="../index.html">Change</a></i></p>
				<div class="header1">Your activity</div>
				<p class="text">Please select one of the following type of sights, which you want to see on your way:</p>
				
                                <table class="selecttable">
                                    <form id="formFilters" name="formFilters" action="ajax.php" method="GET">
                                        <tr><td><input type="checkbox" class="selectFeature" name="filter[]" value="AMUS" checked/> Amusement Park</td>
                                            <td><input type="checkbox" class="selectFeature" name="filter[]" value="MNMT" /> Monument</td>
                                            <td><input type="checkbox" class="selectFeature" name="filter[]" value="MUS" /> Museum</td>
                                            <td> <input type="checkbox" class="selectFeature" name="filter[]" value="OBPT" /> Observation point</td>
                                            <td><input type="checkbox" class="selectFeature" name="filter[]" value="HSTS" checked/> Historical Site</td>
                                            <td><input type="checkbox" class="selectFeature" name="filter[]" value="PRK" /> Park</td>
                                            <td><input type="checkbox" class="selectFeature" name="filter[]" value="ZOO" /> Zoo</td>
                                            <td><input type="checkbox" class="selectFeature" name="filter[]" value="CH" /> Church</td><td></td>
                                            <td><input type="checkbox" class="selectFeature" name="filter[]" value="STDM" checked/> Stadium</td><td></td>
                                            <td><input type="checkbox" class="selectFeature" name="filter[]" value="CSTL" /> Castle</td><td></td></tr>                                            
                                    </form>
                                </table>

			</div>
			<div class="road">
				<div class="box1">
					<div class="header2">Road Connection</div>
					<p class="text">Please select, which sight you want to visit:</p>
					<table class="resulttable" id="resultCar">
						<tr>
							<th width="20"></th>
							<th>Name</th>
							<th>Extra way</th>
							<th>Type</th>
						</tr>	
						<?
						$resavail=false;
						foreach($roadresult as $key=>$r) {
						if($r['length']>0) {
						$resavail=true;
						?>
						<tr>
							<td width="20"><input type="radio" name="road" class="radioRoad" value="<?echo $r['lat'] .'+'.$r['lng'];?>" <? if(!$key) echo "checked" ?>/></td>
							<td><a href="<?echo $r['lat'] .'+'.$r['lng'];?>" class="dialogLink"><? print($r['name']); ?></a></td>
							<td><? print(round(($r['length']/1000),1)."km"); ?></td>
							<td><? print($r['fcode']); ?></td>
						</tr>	
						<? } }
						if(!$resavail) { print "<tr><td colspan=\"4\">Route between ".$from." and ".$to." could not been calculated. </td></tr>"; }
						 ?>
					</table>
					<div class="breakline"></div>
					<p class="text">Here is your connection:</p>
                                        <div id="map_canvas" style="width:400px;height:300px;margin-left:80px;margin-top:20px;margin-bottom:20px;"></div>
                                        <div id="dir_panel"></div>
				</div>
			</div>
			<div class="rail">
				<div class="box1">
					 <div class="header2">Rail Connection</div>
                                        <p class="text">Please select, which sight you want to visit:</p>
                                        <table class="resulttable" id="resultRail">
                                                <tr>
                                                        <th width="20"></th>
                                                        <th>Name</th>
                                                        <th>Extra way</th>
                                                        <th>Type</th>
                                                </tr>
						<?
						if(sizeof($railresult)==0) { print "<tr><td colspan=\"5\">At least one of your locations does not have a railway station.</td></tr>"; }
						else {
						foreach($railresult as $key=>$r) { 
                                                ?>

						<tr>
                                                        <? $coordinates=$myrarar->getStationLatLong($r['beststation']); ?>
                                                        <td width="20"><input type="radio" name="radioTrain" class="radioTrain" value="<? echo $r['lat'] . '+' . $r['lng'].'+'.$coordinates[0].'+'.$coordinates[1].'+'.$r['name'].'+'.$coordinates[2];?>" <? if(!$key) echo "checked" ?>/></td>
                                                        <td><a href="<?echo $r['lat'] .'+'.$r['lng'];?>" class="dialogLink"><? print($r['name']); ?></a></td>
                                                        <td><? print(round(($r['moreway']),1)."km"); ?></td>
                                                        <td><? print($r['fcode']); ?></td>
                                                </tr>	
						<? } } ?>
                                        </table>
                                        <div class="breakline"></div>
                                        <p class="text">Here is your connection:</p>

        <div id="divTrain" style="width: 558px;">
            <div class="ui-accordion ui-widget ui-helper-reset">
                <h3 class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top">
                    <a class="contentHeader">Train route</a>                </h3>
            </div>
            <div id="trainContent" class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">
                <?php
                if ($trainError == '') {
                    //No errors
                    echo '<table><tr><th>Station</th><th>Time</th><th>Platform</th></tr>';
                    
                    foreach ($trainStops as $station) {
                        //Create a more readable date
                        $date = new DateTime($station[1]);

                        //output the data
                        echo '<tr><td> ' . $station[0] . '</td><td> ' . date_format($date, "H:i d-m-Y") . ' </td><td> ' . $station[2] . ' </td></tr>';
                    }
                    echo '</table>';
                } else {
                    //There was a error
                    echo $trainError;
                }
                ?>
            </div>
        </div>
                                        
<p align="text" style="text-align:center"><div id="infotext" style="text-align: center"></div></p>
<div id="map_canvas_train" style="width:400px;height:300px;margin-left:80px;margin-top:20px;margin-bottom:20px;"></div>
                                        <div id="dir_panel_train"></div>
				</div>
			</div>
			<div style="clear:both;"></div>
		</div>
	</div>
	<p align="text" style="text-align:center">&copy; by Maximilian Einoeder and Mark Mensing, 2012.</p>


</body>
</html>
